package org.shilong.UI.main;

import org.shilong.UI.R;
import org.shilong.UI.businessCard.ShowBusinessCardFrame;
import org.shilong.UI.friend.FriendCenterFrame;
import org.shilong.UI.party.PartyCenterFrame;
import org.shilong.ViewUtility.MTitleProvider;
import org.taptwo.android.widget.TitleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MainFrame extends ActivityGroup implements OnClickListener {

	private ViewFlow viewFlow;

	private TitleFlowIndicator indicator;

	private LocalActivityManager manager;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_main_content);

		initViews();

		registerEvents();

	}

	private void initViews() {

		viewFlow = (ViewFlow) this.findViewById(R.id.viewflow);

		indicator = (TitleFlowIndicator) this.findViewById(R.id.viewflowindic);

		manager = getLocalActivityManager();

	}

	private void registerEvents() {

		Class<?>[] arr = new Class<?>[] { FriendCenterFrame.class,
				PartyCenterFrame.class, ShowBusinessCardFrame.class };

		viewFlow.setAdapter(new LayoutAdapter(arr));

		indicator.setTitleProvider(new MTitleProvider());
		viewFlow.setFlowIndicator(indicator);

		viewFlow.setOnViewSwitchListener(new ViewFlow.ViewSwitchListener() {

			@Override
			public void onSwitched(View view, int position) {
				// TODO Auto-generated method stub

				System.out.println("position:" + position);

				if (position == 0) {
					/*
					 * System.out.println("发送刷新广播..."); Intent mIntent = new
					 * Intent( PartyCenterFrame.ACTION_NAME );
					 * 
					 * MainFrame.this.sendBroadcast(mIntent);
					 */

				} else if (position == 1) {

				}

			}
		});

	}

	@Override
	public void onClick(View v) {

	}

	/**
	 * 布局迭代器
	 */

	private class LayoutAdapter extends BaseAdapter {

		private Class<?>[] clsArr;

		public LayoutAdapter(Class<?>[] arr) {

			this.clsArr = arr;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return clsArr.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return clsArr[arg0];
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {

			Intent mIntent = new Intent();

			mIntent.setClass(MainFrame.this, clsArr[arg0]);

			View insideView = manager.startActivity("id" + Math.random(),
					mIntent).getDecorView();

			return insideView;
		}
	}

}
